package com.dongshan.myregistry.controller;



import com.dongshan.myregistry.beans.InstanceMeta;
import com.dongshan.myregistry.cluster.Cluster;
import com.dongshan.myregistry.cluster.Server;
import com.dongshan.myregistry.service.RegisterService;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping(value = "/myregistercontroller")
public class MyRegisterController {

    private static final Logger log = LoggerFactory.getLogger(MyRegisterController.class);
    @Resource
    private RegisterService registryService;
    @Resource
    private Cluster cluster;


    @PostMapping(value = "/register")
    public InstanceMeta register(@RequestParam(value = "server") String server,@RequestBody InstanceMeta instanceMeta){
        log.info("==> register server:{},instance:{}",server,instanceMeta);
        return registryService.register(server,instanceMeta);
    }

    @PostMapping(value = "/unregister")
    public InstanceMeta unregister(@RequestParam(value = "server") String server,@RequestBody InstanceMeta instanceMeta){
        log.info("==> unregister server:{},instance:{}",server,instanceMeta);
        return registryService.unregister(server,instanceMeta);
    }

    @GetMapping(value = "/findAllInstances")
    public List<InstanceMeta> findAllInstances(@RequestParam(value = "server") String server){
        log.info("==> list all Instance——server:{}",server);
        return registryService.findAllInstances(server);
    }

    /**
     * 刷新 servers 和 instanceMeta 的时间戳
     * @param servers
     * @param instanceMeta
     * @return
     */
    @PostMapping(value = "/renews")
    public Long renews(@RequestParam(value = "servers") String servers,@RequestBody InstanceMeta instanceMeta){
        List<String> serverList = Lists.newArrayList(Splitter.on(",").omitEmptyStrings().trimResults()
                .split(Optional.ofNullable(servers).orElse("")));
        log.info("==> renew servers:{}",serverList);
        return registryService.renew(instanceMeta,serverList.toArray(new String[0]));
    }

    /**
     * 获取服务 server 的版本
     * @param server
     * @return
     */
    @GetMapping(value = "version")
    public Long version(@RequestParam(value = "server") String server){
        log.info("==> version server:{}",server);
        return registryService.version(server);
    }

    /**
     * 获取多个server的版本
     * @param servers
     * @return
     */
    @GetMapping(value = "versions")
    public Map<String,Long> versions(@RequestParam(value = "servers") String servers){
        List<String> serverList = Lists.newArrayList(Splitter.on(",").omitEmptyStrings().trimResults()
                .split(Optional.ofNullable(servers).orElse("")));
        log.info("==> version servers:{}",serverList);
        return registryService.versions(serverList.toArray(new String[0]));
    }

    @GetMapping(value = "/info")
    public Server info(){
        log.debug("==> info:{}",cluster.self());
        return cluster.self();
    }

    @GetMapping(value = "/leader")
    public Server leader(){
        log.debug("===> leader: {}",cluster.leader());
        return cluster.leader();
    }

    @GetMapping(value = "/cluster")
    public List<Server> cluster(){
        log.debug("===> cluster: {}",cluster.getServerList());
        return cluster.getServerList();
    }

    @GetMapping("/sf")
    public Server sf(){
        cluster.self().setLeader(true);
        log.debug("===> sf: {}",cluster.self());
        return cluster.self();
    }


}
